
import s from "./PlayerDataSys"
import c from "./AbTestMgr"
import u from "./OutsideGuideExperimentMgr"
import p from "./PeripheralSystemDataMgr"
import d from "./EventMgr"
import h from "./GameEventType"
import f from "./frameworkManager"
import g from "./ScrollViewCtrl"
import y from "./SdkHelper"
import m from "./EngineUtil"
import _, { AnimType } from "./BasePageCtrl"
import v from "./PeripheralSystemMgr"
import b from "./UiManage"
import C from "./ClickEveryDayPage"
import T from "./ClickEveryDayItemCtrl"
import BasePageCtrl from "./BasePageCtrl"

const { ccclass, property } = cc._decorator;
@ccclass
export default class ClickEveryDayPageCtrl extends BasePageCtrl {
    
    public progress = null;
    public task_list = [];
    public clock_days = null;
    public task_id = null;
    public guideEnter = false;
    public guideEcperiment = false;
    public showToBeijingAni = false;

    public _animType = AnimType.NONE;
    public _touchControl = true;
    public _hasPeneLock = true;
    public _hasBlack = true;
    public _hasTouchLock = true;
    public _hasBlackTouch = false;
    public showWinSize = true;

    public onLoad() {
        this.onUILoad();
        this._animType =AnimType.NONE;
        this._touchControl = true;
        this._hasPeneLock = true;
        this._hasBlack = true;
        this._hasTouchLock = true;
        this._hasBlackTouch = false;
        this._showWinSize = true;
        super.onLoad();
        this.addButtonListen();
        this.addEvent();
    };
    public start() { };
    public _init(e, t) {
        void 0 === t && (t = false);
        if (e) {
            var a = c.isOptimizeTravelFundFlag();
            this.ui.cashNode.x = a ? 152 : -138;
            f.log("每日打卡界面数据", e);
            this.progress = e.progress;
            this.task_list = e.task_list;
            this.clock_days = e.clock_days;
            this.task_id = this.progress.task_id;
            this.initTopCash();
            this.initTopUI();
            if (!t) {
                this.guideEnter = e.guideEnter;
                this.guideEcperiment = e.guideEcperiment;
                this.initFixedItem();
                this.addCenterItem();
            }
            this.checkGuide(t);
            this.showToBeijingAni = false;
        } else f.log("每日打卡界面数据不正确请检查");
    };
    public checkGuide(e) {
        void 0 === e && (e = false);
        if (this.guideEnter || this.guideEcperiment) if (e) {
            this.setGuideCanTouch(true);
            this.ui.guide_hand.active = false;
        } else {
            this.setGuideCanTouch(false);
            this.ui.guide_hand.active = true;
        } else this.setGuideCanTouch(true);
    };
    public setGuideCanTouch(e) {
        this.ui.maskNode.active = !e;
        this.ui.cashNode.getComponent(cc.Button).interactable = e;
        this.ui.btn_close.getComponent(cc.Button).interactable = e;
    };
    public initTopCash() {
        this.ui.cashCount.getComponent(cc.Label).string = m.getCashBalance(s.cash_balance) + "元";
        this.checkCanWithDraw();
    };
    public updateUserCash(e) {
        var t = this;
        void 0 === e && (e = null);
        this.ui.cashCount.getComponent(cc.Label).string = m.getCashBalance(s.cash_balance) + "元";
        if (null != e) {
            if (e) {
                var a = e.reward, o = e.isAdd;
                this.ui.redPaperRewardCount.getComponent(cc.Label).string = "+" + a;
                this.ui.redPaperRewardNode.active = true;
                o && cc.tween(this.ui.redPaperRewardNode).to(1.5, {
                    opacity: -100
                }).call(function () {
                    t.ui.redPaperRewardNode.opacity = 255;
                    t.ui.redPaperRewardNode.active = false;
                    t.checkCanWithDraw();
                }).start();
            }
        } else this.checkCanWithDraw();
    };
    public checkCanWithDraw() {
        if (p.isCanWithDraw()) {
            this.ui.btn_payment.active = false;
            this.ui.btn_canPay.active = true;
        } else {
            this.ui.btn_payment.active = true;
            this.ui.btn_canPay.active = false;
        }
    };
    public clickWithDraw() {
        v.getExtractInfo();
    };
    public playCashNodeAction() {
        var e = this;
        cc.tween(this.ui.cashNode).to(.05, {
            scale: 1.1
        }).to(.05, {
            scale: 1
        }).call(function () {
            e.ui.cashNode.scale = 1;
        }).start();
    };
    public onUILoad() {
        this.ui = this.node.addComponent(C);
    };
    public addEvent() {
        d.listen(h.UPDATE_USER_CASH, this.updateUserCash, this);
        d.listen(h.CASH_AMPLIFY, this.playCashNodeAction, this);
    };
    public removeEvent() {
        d.ignore(h.UPDATE_USER_CASH, this.updateUserCash, this);
        d.ignore(h.CASH_AMPLIFY, this.playCashNodeAction, this);
    };
    public initTopUI() {
        var e = this, t = this.progress.current / this.progress.total, a = this.progress.get_status, o = this.progress.reward, i = this.progress.reward_type;
        if (1 == i) {
            var r = m.getCashBalance(o);
            this.ui.rewardDes.getComponent(cc.Label).string = r + "元";
        } else this.ui.rewardDes.getComponent(cc.Label).string = "*" + o;
        var n = "reward_" + i;
       UiManager.loadSpriteFrame(this.ui.redPaper, "clickEveryDay", n, function () {
            e.ui.redPaper.scale = 1 == i ? .8 : 1;
        });
        if (0 == a) {
            this.ui.leftDoneNode.active = false;
            this.ui.leftNode.active = true;
            var l = "<color=#4484FF>再闯</color><color=#FF0000> " + this.progress.need + " </color><color=#4484FF>关完成今日打卡</color>";
            this.ui.tipsDes.getComponent(cc.RichText).string = l;
            var s = m.getProgressWidth(t, 20, 298).width;
            this.ui.pro.width = s;
            this.ui.progressDes.getComponent(cc.Label).string = this.progress.current + "/" + this.progress.total;
            this.changeBtnState(this.ui.btn_get_reward, 0);
            this.ui.btn_get_reward.getComponent(cc.Button).interactable = false;
        } else {
            this.ui.leftDoneNode.active = true;
            this.ui.leftNode.active = false;
            var c = "<b><color=#1D46A3>您已经累计打卡</color><color=#FC1313> " + this.clock_days + " </color><color=#1D46A3>天</color></b>";
            this.ui.cumulativeDesLabel.getComponent(cc.RichText).string = c;
            if (1 == a) {
                this.changeBtnState(this.ui.btn_get_reward, 2);
                this.ui.btn_get_reward.getComponent(cc.Button).interactable = false;
            } else {
                this.changeBtnState(this.ui.btn_get_reward, 1);
                this.ui.btn_get_reward.getComponent(cc.Button).interactable = true;
            }
        }
    };
    public initFixedItem() {
        return n(this, void 0, void 0, function () {
            var e, t, a, o;
            return l(this, function (i) {
                switch (i.label) {
                    case 0:
                        this.ui.finalFixedItem.removeAllChildren();
                        e = this.getItemDataById("30");
                        return [4,UiManager.loaderPrefab("ClickEveryDayItem")];

                    case 1:
                        t = i.sent();
                        a = cc.instantiate(t);
                        this.ui.finalFixedItem.addChild(a);
                        (o = a.getComponent(T)) || a.addComponent(T);
                        o.initData(e);
                        o.setCenterAlign();
                        return [2];
                }
            });
        });
    };
    public addCenterItem() {
        this.ui.content.removeAllChildren();
        var e = this.getCenterItemData();
        if (!e || e && 0 == e.length) {
            this.adjustFixedItmePos(e);
            f.log("每日打卡数据为空");
        } else {
            this.ui.scrollview.getComponent(g).init(e);
            this.ui.scrollview.getComponent(g).scrollTotop();
            this.adjustFixedItmePos(e);
        }
    };
    public adjustFixedItmePos(e) {
        var t = e.length;
        this.ui.finalFixedItem.y = -435;
        if (t <= 4) {
            var a = 4 - t + 2, o = (a - 1) * this.ui.finalFixedItem.height + (this.ui.finalFixedItem.height - 75) + this.ui.content.getComponent(cc.Layout).spacingY * (a - 1);
            this.ui.finalFixedItem.y += o;
        }
    };
    public getCenterItemData() {
        for (var e = ["30"], t = [], a = 0; a < this.task_list.length; a++) {
            var o = this.task_list[a];
            -1 === e.indexOf(o.task_id) && parseInt(o.task_id) > parseInt(this.task_id) && t.push(o);
        }
        return t;
    };
    public getItemDataById(e) {
        if (null != e) {
            for (var t = 0; t < this.task_list.length; t++) if (this.task_list[t].task_id == e) return this.task_list[t];
            return null;
        }
        f.log("获取数据的index不正确======");
    };
    public addButtonListen() {
       UiManager.addButtonListen(this.ui.btn_get_reward, this.clickReward, this, 0);
       UiManager.addButtonListen(this.ui.btn_close, this.clickClose, this);
       UiManager.addButtonListen(this.ui.cashNode, this.clickWithDraw, this);
    };
    public clickReward() {
        var e = this;
        this.ui.guide_hand.active = false;
        this.ui.btn_get_reward.getComponent(cc.Button).interactable = false;
        v.getClockTaskReward(this.task_id, 0, function (t) {
            e.succeedReward(t);
        });
    };
    public succeedReward(e) {
        if (e && e.clock_days >= 30) {
            this.showToBeijingAni = true;
            y.reportData("clock_day_success", {
                days: 30
            });
            this.clickClose();
        } else {
            if (this.guideEnter) {
                p.setOutsideStep(v.OutSideLevelGuide.CLOCK_DAY);
                v.reportOutsideGuideStep(v.OutSideLevelGuide.CLOCK_DAY);
                p.showOutsideGuidePage();
            }
            if (this.guideEcperiment) {
                p.setOutsideStep(u.OutSideLevelGuideExperimentStep.CLOCK_DAY);
                v.reportOutsideGuideStep(u.OutSideLevelGuideExperimentStep.CLOCK_DAY);
                p.showOutsideGuidePage();
            }
            this.changeBtnState(this.ui.btn_get_reward, 2);
            this._init(e, true);
            var t = this.progress.reward_type, a = this.progress.reward;
            if (1 == t) d.trigger(h.SHOWEFFECT_FLYINGRED, {
                startPos: cc.v2(0, 0),
                type: t - 1,
                callback: function () {
                    s.updateCashBalace(a);
                }
            }); else {
                d.trigger(h.SHOWEFFECT_FLYINGRED, {
                    startPos: cc.v2(0, 0),
                    type: t,
                    callback: function () { },
                    reward: a
                });
                this.clickClose();
            }
        }
    };
    public clickClose() {
        this.showToBeijingAni && d.trigger(h.SHOW_BEIJING_FLAG_ANI);
        this.hide();
    };
    public onDestroy() {
        this.removeEvent();
    };
    public changeBtnState(e, t) {
        if (e) {
            var a = "btn_reward_state_" + t;
           UiManager.loadSpriteFrame(e, "clickEveryDay", a);
        }
    };
    public _onHide() {
        if (this.guideEnter || this.guideEcperiment) {
            c.isNoGqtcFlag() ? d.trigger(h.ENTER_GAME_SHOW) : d.trigger(h.SHOW_GAME_START_PAGE, {
                isNeddPop: true
            });
            d.trigger(h.SET_TOP_NODE_CANTOUCH, {
                isCanTouch: true
            });
        }
        super._onHide.call(this);
        this.ui.scrollview.getComponent(g).recycle();
        this.ui.scrollview.getComponent(g).scrollTotop();
    };
    // t.prefabUrl = "ClickEveryDayPage";
    // t.className = "ClickEveryDayPageCtrl";
    // return r([P, S("UI/pages/ClickEveryDayPageCtrl")], t);
}